/*  BlogBotLoader.java Version 100 - 20th May 2008
 *  BlogBotLoader.java Loads Blog Bot Configuration Data
 *  See Versions for program version number
 *  Copyright (C) 2008  Andy Broomfield andy@newmediathinking.com
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/

package net.blogbotplatform.blogbot;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class BlogBotLoader {
	//file vars
	private static BufferedReader in;
	private static String line = null;
	
	//filenames for config and comments
	static String commentsfile = "comments.dat";
	static String configfile = "settings.dat";
	static String filepath = "config/";
	static String textContents;
	
	//blog bot vars
	static int tense = -1;
	static int mode = -1;

	
	public BlogBotLoader() {
		//intilizeComments();
		System.out.println("Loading Configuration - Constructor.");
	}
	
	static void loadConfig() {
		//initilize and load in the values of the config files, set them to the shop
		intilizeSettings();
		intilizeComments();
		readSettingsFile(filepath + configfile);
		readCommentsFile(filepath + commentsfile);
	}
	
	static void intilizeSettings() {
		//Clear out the config setting, ready for a new set-up
		//it is best to default each set up to false unless needed
		//clear sleep timer
		BlogBotShop.timedSleep = false;
		BlogBotShop.wakeHour = 0;
		BlogBotShop.sleepHour = 0;
		//clear twitter password
		BlogBotShop.useTwitter = false;
		BlogBotShop.twitterUsername = "";
		BlogBotShop.twitterPassword = "";
	}
	
	static void readSettingsFile(String fileName) {
		//reads the config file to set up settings
		System.out.println("start reading text file");
		String textString = "";
	 
		try {
		  //attempt to read in the file
			in = new BufferedReader(new FileReader(fileName));
		  } 
		  catch (IOException e) {
		    System.out.println("problem opening file" + e);
		  }
	
		  try {
		    for(;;) {
		      //read in a new line
		      line = in.readLine();
		      
		      //determine if we are at the end
		      if (line == null) break;
		      //readin each line, check any special indicators that act as switches when parsing the file
		      System.out.println("reading");
		      System.out.println(line);
		      textString += line + "\n";
		      if (line.indexOf("=")!=-1) {
		    	  //checking for an = in a line, which tells us there are vars there.
		    	  parseSetupElement(line);
		      } else {
		    	  //Else we don't know what it is.
		    	  System.out.println("Unknown line in file!");
		      }
		    }
		  } 
		  catch (IOException e) {
		    ;
		  } 
		  finally {
		    try { 
		      in.close(); 
		    } 
		    catch (IOException e2) {
		      ;
		    }
		    System.out.println("finished reading config file");
		  }
	}
	
	static void parseSetupElement(String line) {
		String element = "";
		String value = "";
		int sPos = line.indexOf("=");
		if (sPos!=-1) {
			//split the line into element and value;
			element = line.substring(0, sPos);
			value = line.substring(sPos+1);
			System.out.println("element " + element + " value " + value);
		}
		//now parse the element, and if its a know one, store the value
		if (element.equals("timedSleep")) {
			try {
			BlogBotShop.timedSleep = Boolean.parseBoolean(value);
			} catch(Exception e) {}
		}
		if (element.equals("wakeHour")) {
			try {
				BlogBotShop.wakeHour = Integer.parseInt(value);
				System.out.println("wakeHour " + BlogBotShop.wakeHour);
			} catch(NumberFormatException nfe) {}
		}
		if (element.equals("sleepHour")) {
			try {
				BlogBotShop.sleepHour = Integer.parseInt(value);
				System.out.println("sleepHour " + BlogBotShop.sleepHour);
			} catch(NumberFormatException nfe) {}
		}
		if (element.equals("useTwitter")) {
			try {
			BlogBotShop.useTwitter = Boolean.parseBoolean(value);
			} catch(Exception e) {}
		}
		if (element.equals("twitterUsername")) {
			BlogBotShop.twitterUsername = value;
		}
		if (element.equals("twitterPassword")) {
			BlogBotShop.twitterPassword = value;
		}
	}
	
	static void readCommentsFile(String fileName) { 
		//file vars
		//BufferedReader in;
		//String line = null;
		
		//sets up a blank textString for the file
		System.out.println("start reading text file");
		String textString = "";
	 
		try {
		  //attempt to read in the file
			in = new BufferedReader(new FileReader(fileName));
		  } 
		  catch (IOException e) {
		    System.out.println("problem opening file" + e);
		  }
	
		  try {
		    for(;;) {
		      //read in a new line
		      line = in.readLine();
		      
		      //determine if we are at the end
		      if (line == null) break;
		      //readin each line, check any special indicators that act as switches when parsing the file
		      System.out.println("reading");
		      System.out.println(line);
		      textString += line + "\n";
		      if (line.indexOf("*")!= -1) {
		    	  //* means different handler (past or live comments)
		    	  parseHandler(line);
		      } else if (line.indexOf("$")!=-1) {
		    	  //$ means different variations (children) of the handler, (different modes of past and live comments)
		    	  parseChild(line);
		      } else {
		    	  //empty means this is a comment to be read in (if we are in a comment handler!)
		    	  addNewComment(line);
		      }
		    }
		  } 
		  catch (IOException e) {
		    ;
		  } 
		  finally {
		    try { 
		      in.close(); 
		    } 
		    catch (IOException e2) {
		      ;
		    }
		    System.out.println("finished reading text file");
		  }
		  
		  //return textString;
		  //System.out.println(fileList);
		}
	
	static void intilizeComments() {
		//clear out the entire array of comments ready for a new set.
		for (int i = 0; i < BlogBotShop.NUM_OF_MODES; i++) {
			BlogBotShop.PAST_COMMENTS[i].clear();
			BlogBotShop.LIVE_COMMENTS[i].clear();
		}
	}
	
	static void parseHandler(String handler) {
		//changes the handlers. these are simple flags so that comments and other items are added in the right place
		System.out.println("looking at handlers");
		if (handler.equals("*pastComments")) { System.out.println("past tense"); tense = 0; }
		if (handler.equals("*liveComments")) { System.out.println("live tense"); tense = 1; }
	}
	
	static void parseChild(String child) {		 
		//a child of each handler, again to set flags so comments are added to the right place
		System.out.println("looking at children");
		if (child.indexOf("mode")!=-1) {
			//if the child is a mode, determine which mode it is and change the mode switch
			System.out.println("looking at mode");
			int sPos = child.indexOf("=");
			if (sPos!=-1) {
				try {
					mode = Integer.parseInt(child.substring(sPos+1));
					System.out.println("mode " + mode);
				} catch(NumberFormatException nfe) {}
			}
		}
	}
	
	static void addNewComment(String comment) {
		//System.out.println(comment);
		//add a new comment, using the previous settings will put it in the right place
		//System.out.println(mode);
		if ((mode > BlogBotShop.NUM_OF_MODES) || (mode < 0)) {
			//if the mode is outside the bounds, ignore this comment
			System.out.println("Outside Mode Bounds");
			return;
		}
		if ((tense > 1) || (tense < 0)) {
			//incorrect tense setting, ignore comment
			System.out.println("Incorrect Tense");
			return;
		}
		if (tense == 0) {
			//past tense, store comment in PAST_COMMENTS arraylist
			BlogBotShop.PAST_COMMENTS[mode].add(comment);
		}
		if (tense == 1) {
			//present tense, store comment in LIVE_COMMENTS arraylist
			BlogBotShop.LIVE_COMMENTS[mode].add(comment);
		}
		System.out.println("added comment");
	}
}
